<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Article;
use App\Http\Model\Cate;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Services\OSS;



class ArticleController extends Controller
{
//    文件上传
    public function upload(Request $request)
    {

//        获取文件对象
        $file = Input::file('file_upload');
       // dd($file);
//        判断文件是否有效
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;

//            1上传到本地服务器
            $path = $file->move(public_path().'/uploads',$newName);
//            2上传到七牛云
//            writeStream（要上传的七牛存储空间的路径，你要上传的文件当前服务器上的路径）
//            \Storage::disk('qiniu')->writeStream('uploads/'.$newName, fopen($file->getRealPath(), 'r'));


            //3阿里 OSS 图片上传
            // 获取图片在临时文件中的地址
//            $pic = $file->getRealPath();
//            $result = OSS::upload('uploads/'.$newName, $pic);

            $filepath = 'uploads/'.$newName;
            return  $filepath;
        }
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $keyword =  $request->input('keywords','');
        $data =  Article::where('art_title','like','%'.$keyword.'%')->paginate(5);
        return view('admin.article.index',compact('data','keyword'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //获取分类数据
        $cates = (new Cate())->getTree();
        return view('admin.article.add',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //获取到用户提交过来的数据
        $input = $request->except('_token','file_upload','editorValue');
        $input['art_time'] = time();
        //表单验证

        $re = Article::create($input);

        if($re){
            return redirect('admin/article');
        }else{
            return back()->with('errors','添加失败');
        }


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $cates = (new Cate())->getTree();
        $art = Article::find($id);
        return view('admin.article.edit',compact('art','cates'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $input = Input::except('_token','_method');
        
        $art = Article::find($id);
        $re =  $art->update($input);
        if($re){
            return redirect('admin/article');
        }else{
            return back()->with('errors','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $re =  Article::find($id)->delete();
        $data = [];
        if($re){
            $data['status']= 0;
            $data['msg']='删除成功';
        }else{
            $data['status']= 1;
            $data['msg']='删除失败';
        }
        return $data;
    }
}
